package org.example.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 张琪
 * @date 2024/4/19 14:14
 */
public class AwaitSiginalExample {

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void before() {
        lock.lock();
        try {
            System.out.println("before");
            condition.signalAll();
            System.out.println("结束条件等待");
        } finally {
            lock.unlock();
        }
    }


    public void after() {
        lock.lock();
        try {
            System.out.println("开始条件等待");
            condition.await();
            System.out.println("after");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        AwaitSiginalExample example = new AwaitSiginalExample();
        new Thread(example::after).start();
        new Thread(example::before).start();
    }


}
